package com.example.pharmaceuticalsales.Controller;

import com.example.pharmaceuticalsales.Model.FinancialStats;
import com.example.pharmaceuticalsales.Model.MonthlyStats;
import com.example.pharmaceuticalsales.Model.YearlyStats;
import com.example.pharmaceuticalsales.Service.FinancialStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;


@CrossOrigin("*")
@RestController
@RequestMapping("/api/financialstats")
public class FinancialStatsController {

    private final FinancialStatsService financialStatsService;

    @Autowired
    public FinancialStatsController(FinancialStatsService financialStatsService) {
        this.financialStatsService = financialStatsService;
    }

    @GetMapping
    public ResponseEntity<Page<FinancialStats>> getAllFinancialStats(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "createdAt") String sortBy) {
        return ResponseEntity.ok(financialStatsService.getAllFinancialStats(page, size, sortBy));
    }

    @GetMapping("/byDate")
    public ResponseEntity<Page<FinancialStats>> getFinancialStatsByDate(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date statsDate,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ResponseEntity.ok(financialStatsService.getFinancialStatsByDate(statsDate, page, size));
    }

    @PostMapping
    public ResponseEntity<FinancialStats> createFinancialStats(@RequestBody FinancialStats financialStats) {
        return ResponseEntity.ok(financialStatsService.saveFinancialStats(financialStats));
    }

    @PutMapping("/{id}")
    public ResponseEntity<FinancialStats> updateFinancialStats(@PathVariable Long id, @RequestBody FinancialStats financialStats) {
        return ResponseEntity.ok(financialStatsService.updateFinancialStats(id, financialStats));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFinancialStats(@PathVariable Long id) {
        financialStatsService.deleteFinancialStats(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/monthlyStats")
    public ResponseEntity<List<MonthlyStats>> getMonthlyStatsByYear(@RequestParam int year) {
        return ResponseEntity.ok(financialStatsService.getMonthlyStatsByYear(year));
    }

    @GetMapping("/yearlyStats")
    public ResponseEntity<List<YearlyStats>> getYearlyStatsByYear(@RequestParam int year) {
        return ResponseEntity.ok(financialStatsService.getYearlyStatsByYear(year));
    }

    @GetMapping("/byMonth")
    public ResponseEntity<List<FinancialStats>> getFinancialStatsByMonth(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate) {
        return ResponseEntity.ok(financialStatsService.getFinancialStatsByMonth(startDate, endDate));
    }
}
